package org.linlinjava.litemall.admin.web;

import io.swagger.models.auth.In;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallBookList;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallBookList;
import org.linlinjava.litemall.db.service.LitemallBookListService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/bookList")
@Validated
public class AdminBookListController {
    private final Log logger = LogFactory.getLog(AdminBookListController.class);

    @Autowired
    private LitemallBookListService bookListService;
    @Autowired
    private LitemallGoodsService goodsService;

    /**
     * 获取书单列表
     * @param title
     * @param subtitle
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    public Object list(String title, String subtitle,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort(accepts = {"id", "add_time", "price"}) @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallBookList> bookLists = bookListService.querySelective(title, subtitle, page, limit, sort, order);
        return ResponseUtil.okList(bookLists);
    }

    private Object validate(LitemallBookList bookList) {
        String title = bookList.getTitle();
        if (StringUtils.isEmpty(title)) {
            return ResponseUtil.badArgument();
        }
        String content = bookList.getContent();
        if (StringUtils.isEmpty(content)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    /**
     * 创建一个书单
     * @param bookList
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody LitemallBookList bookList) {
        Object error = validate(bookList);
        if (error != null) {
            return error;
        }
        bookListService.add(bookList);
        return ResponseUtil.ok(bookList);
    }

    /**
     * 读取某个书单
     * @param id
     * @return
     */
    @GetMapping("/read")
    public Object read(@RequestParam Integer id) {
        LitemallBookList bookList = bookListService.findById(id);
        Integer[] goodsIds = bookList.getGoods();
        List<LitemallGoods> goodsList = null;
        if (goodsIds == null || goodsIds.length == 0) {
            goodsList = new ArrayList<>();
        } else {
            goodsList = goodsService.queryByIds(goodsIds);
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("bookList", bookList);
        data.put("goodsList", goodsList);
        return ResponseUtil.ok(data);
    }

    /**
     * 修改某个书单
     * @param bookList
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody LitemallBookList bookList) {
        Object error = validate(bookList);
        if (error != null) {
            return error;
        }
        if (bookListService.updateById(bookList) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(bookList);
    }

    /**
     * 删除某个书单
     * @param bookListId
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody Integer bookListId) {
        bookListService.deleteById(bookListId);
        return ResponseUtil.ok();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/batch-delete")
    public Object batchDelete(@RequestBody Integer[] ids) {
        bookListService.deleteByIds(ids);
        return ResponseUtil.ok();
    }
}